#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题: 可接受任意数量参数的函数
问题: 你想构造一个可接受任意数量参数的函数。
提示 : 
"""

import html

def recipe1():
    print(f"{avg(1, 2) = }")
    print(f"{avg(1, 2, 3, 4) = }")

def recipe2():
    a = make_element('item', 'Albatross', size='large', quantity=6)
    b =make_element('p', '<span>')
    print(f"{a = }")
    print(f"{b = }")

def anyargs(*args, **kwargs):
    '''某个函数能同时接受任意数量的位置参数和关键字参数'''
    print(args)
    print(kwargs)

def avg(first, *rest):
    return (first + sum(rest)) / (1 + len(rest))

def make_element(name, value, **attrs):
    '''接受任意数量的关键字参数'''
    keyvals = [' %s=%s' % item for item in attrs.items()]
    attr_str = ''.join(keyvals)
    element = '<{name}{attrs}>{value}</{name}>'.format(
                name=name,
                attrs=attr_str,
                value=html.escape(value))
    return element

def main():
    print('recipe1'.center(20, '*'))
    recipe1()
    print('recipe2'.center(20, '*'))
    recipe2()

if __name__ == '__main__':
    main()    